package com.eascs.loan.controller;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.common.entity.CustomerBasicInfoEntity;
import com.eascs.crm.common.service.ICustomerInfoService;
import com.eascs.crm.common.vo.QueryCustomerVO;
import com.eascs.loan.entity.O2oPaymentsPlan;
import com.eascs.loan.form.O2oPaymentsForm;
import com.eascs.loan.service.O2oPaymentsService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxConfirmButton;
import com.eascs.tag.MenuTag;

/**
 * @ClassName: O2oPaymentsController
 * @Description: 还款管理
 * @author 张庆奇
 * @date 2016年6月16日 下午5:53:45
 * @version V1.0
 */
@Controller
@RequestMapping("/payments")
public class O2oPaymentsController extends BaseMasterViewController {

	@Inject
	O2oPaymentsService o2oPaymentsService;
	@Autowired
	private O2oLoanapplyService loanService;
	@Reference
	private ICustomerInfoService customerInfoService;

	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "bankDataManage");
	}

	/**
	 * @Title: paymentsList
	 * @Description: 还款计划列表
	 * @author 张庆奇
	 * @param o2oPaymentsForm
	 * @return
	 * @return String
	 * @throws @date
	 *             2016年6月17日 上午9:33:42
	 */
	@RequestMapping("/payments_list")
	public String paymentsList(O2oPaymentsForm o2oPaymentsForm) {
		o2oPaymentsForm.setPageSize(10);
		PageResult<Object[]> pageResult = o2oPaymentsService.paymentsList(o2oPaymentsForm);
		String[] titles = { "贷款单号", "借款人姓名","借款人证件号","还款期数", "应还款日期", "剩余本金", "应还本金", "应还利息","本期应还款" };
		String findCondition = "";
		String pageUrl = "/payments/payments_list";

		if (o2oPaymentsForm != null) {
			String billCode = o2oPaymentsForm.getBillCode() == null ? "" : o2oPaymentsForm.getBillCode();
			String beginPaymentsDate = o2oPaymentsForm.getBeginPaymentsDate() == null ? ""
					: o2oPaymentsForm.getBeginPaymentsDate();
			String endPaymentsDate = o2oPaymentsForm.getEndPaymentsDate() == null ? ""
					: o2oPaymentsForm.getEndPaymentsDate();
			findCondition = "?billCode=" + billCode + "&beginPaymentsDate=" + beginPaymentsDate + "&endPaymentsDate="
					+ endPaymentsDate;
			pageUrl = pageUrl + findCondition;
		}
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl + "&showPage=");
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		AjaxConfirmButton[] dealButton = new AjaxConfirmButton[] { new AjaxConfirmButton("删除", "/paymentsRest/delPayments?payments_id=") };
		paginationInfo.setMoreButtons(dealButton);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", o2oPaymentsForm);
		BaseRestResult.refresh();
		return "/bank/payments_list";
	}

	/**
	 * @Title: addPayments
	 * @Description: 添加页面
	 * @author 张庆奇
	 * @param o2oLoanForm
	 * @return
	 * @return String
	 * @throws @date
	 *             2016年6月17日 上午9:33:52
	 */
	@RequestMapping("/add_payments")
	public String addPayments(String billCode) {
		O2oPaymentsPlan plan=new O2oPaymentsPlan();

		if (StringUtil.isNotBlank(billCode)) {
			plan.setBillCode(billCode);
			O2oLoanapplyEntity loanapply = loanService.fingByBillCode(billCode);
			if (loanapply == null) {
				throw new RuntimeException("贷款单号不存在");

			}else{
				QueryCustomerVO vo=new QueryCustomerVO();
				vo.setCustomer_id(loanapply.getLoanId());
		        CustomerBasicInfoEntity	info=customerInfoService.getCustomerBasicInfoEntity(vo);
		        if(info!=null){
		        	plan.setName(info.getCustomerName());
		        	plan.setIdNumber(info.getIdNumber());
		        }else{
		        	throw new RuntimeException( "借款人信息不存在");
		        	
		        }
				
			}
			
		}
		
		ViewContext.put("plan", plan);
		return "/bank/add_payments";
	}

	
	
}
